package com.mhc.suzuki.service.impl;

import com.mhc.suzuki.constant.IsDeletedEnum;
import com.mhc.suzuki.dal.model.CarInfoDO;
import com.mhc.suzuki.dal.model.LoanInfoDO;
import com.mhc.suzuki.dal.model.OrderDO;
import com.mhc.suzuki.dal.query.LoanInfoQuery;
import com.mhc.suzuki.manager.LoanInfoManager;
import com.mhc.suzuki.manager.OrderManager;
import com.mhc.suzuki.service.CarInfoService;
import com.mhc.suzuki.service.LoanInfoService;
import com.mhc.suzuki.util.BeanCopierUtil;
import com.mhc.suzuki.vo.LoanInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class LoanInfoServiceImpl implements LoanInfoService {

    @Autowired
    private LoanInfoManager loanInfoManage;
    @Autowired
    private CarInfoService carInfoService;
    @Autowired
    private OrderManager orderManager;


    @Override
    public LoanInfoVO getLoanInfo(String orderNO) {
        LoanInfoQuery loanInfoQuery = new LoanInfoQuery();
        loanInfoQuery.createCriteria().andOrderNoEqualTo(orderNO).andIsDeletedEqualTo(IsDeletedEnum.NORMAL.getCode());
        List<LoanInfoDO> loanInfoDOList = loanInfoManage.selectByQuery(loanInfoQuery);
        if (!CollectionUtils.isEmpty(loanInfoDOList)) {
            LoanInfoDO loanInfoDO = loanInfoDOList.get(0);
            return BeanCopierUtil.convert(loanInfoDO, LoanInfoVO.class);
        } else {
            LoanInfoVO loanInfoVO = new LoanInfoVO();
            OrderDO orderDO = orderManager.selectByOrderNo(orderNO);
            if (orderDO != null) {
                loanInfoVO.setBankId(orderDO.getLoanBankId());
            }
            return loanInfoVO;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addOrUpdateLoanInfo(LoanInfoDO loanInfoDO) {
        Boolean result;
        LoanInfoQuery loanInfoQuery = new LoanInfoQuery();
        loanInfoQuery.createCriteria().andOrderNoEqualTo(loanInfoDO.getOrderNo()).andIsDeletedEqualTo(IsDeletedEnum.NORMAL.getCode());
        int count = loanInfoManage.countByQuery(loanInfoQuery);
        if (count > 0) {
            int updateCount = loanInfoManage.updateByQuerySelective(loanInfoDO, loanInfoQuery);
            if (updateCount > 0) {
                result = Boolean.TRUE;
            } else {
                result = Boolean.FALSE;
            }
        } else {
            loanInfoManage.insertSelective(loanInfoDO);
            result = Boolean.TRUE;
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addOrUpdateLoanInfoAndCarInfo(LoanInfoDO loanInfoDO, CarInfoDO carInfoDO, Long staffId) {
        log.info("addOrUpdateLoanInfoAndCarInfo");
        this.addOrUpdateLoanInfo(loanInfoDO);
        Boolean result = carInfoService.addOrUpdateCarInfo(carInfoDO, staffId);
        return result;
    }
}
